package io.renren.modules.sys.controller;

import java.util.Arrays;
import java.util.Map;

import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.AddGroup;
import io.renren.common.validator.group.UpdateGroup;
import io.renren.modules.sys.shiro.ShiroUtils;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import io.renren.modules.sys.entity.JxLaboratoryManagerEntity;
import io.renren.modules.sys.service.JxLaboratoryManagerService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 实验室管理员
 *
 * @author Mark
 * @email sunlightcs@gmail.com
 * @date 2021-10-25 12:55:29
 */
@RestController
@RequestMapping("sys/jxlaboratorymanager")
public class JxLaboratoryManagerController {
    @Autowired
    private JxLaboratoryManagerService jxLaboratoryManagerService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:jxlaboratorymanager:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = jxLaboratoryManagerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:jxlaboratorymanager:info")
    public R info(@PathVariable("id") Long id){
        JxLaboratoryManagerEntity jxLaboratoryManager = jxLaboratoryManagerService.getById(id);

        return R.ok().put("jxLaboratoryManager", jxLaboratoryManager);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody JxLaboratoryManagerEntity jxLaboratoryManager){
        ValidatorUtils.validateEntity(jxLaboratoryManager,AddGroup.class);
        String salt = RandomStringUtils.randomAlphanumeric(20);
        jxLaboratoryManager.setSalt(salt);
        jxLaboratoryManager.setPassword(ShiroUtils.sha256(jxLaboratoryManager.getPassword(), jxLaboratoryManager.getSalt()));
        jxLaboratoryManagerService.save(jxLaboratoryManager);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody JxLaboratoryManagerEntity jxLaboratoryManager){
        ValidatorUtils.validateEntity(jxLaboratoryManager,UpdateGroup.class);
        JxLaboratoryManagerEntity jxLaboratoryManager1=jxLaboratoryManagerService.getById(jxLaboratoryManager.getId());
        if(!(jxLaboratoryManager.getPassword().equals(jxLaboratoryManager1.getPassword()))){
           jxLaboratoryManager.setPassword(ShiroUtils.sha256(jxLaboratoryManager.getPassword(), jxLaboratoryManager.getSalt()));
        }
        jxLaboratoryManagerService.updateById(jxLaboratoryManager);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:jxlaboratorymanager:delete")
    public R delete(@RequestBody Long[] ids){
        jxLaboratoryManagerService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }


    /**
     * 信息
     */
    @RequestMapping("/managerInfo/{laboratoryId}")
    public R mangerInfo(@PathVariable("laboratoryId") Long laboratoryId){
        JxLaboratoryManagerEntity jxLaboratoryManager = jxLaboratoryManagerService.getlaboratoryId(laboratoryId);
        return R.ok().put("jxLaboratoryManager", jxLaboratoryManager);
    }

}
